package io.renren.modules.sq.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ResponseData;
import io.renren.modules.sq.dto.SysAuthDto;
import io.renren.modules.sq.dto.VerifyDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sq.entity.SqSystemAuthorizationCodeEntity;
import io.renren.modules.sq.service.SqSystemAuthorizationCodeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 系统授权码表
 *
 * @author txw_cyl
 * @email sunlightcs@gmail.com
 * @date 2022-05-14 19:29:39
 */
@Api(tags = "授权码")
@RestController
@RequestMapping("sys/sqsystemauthorizationcode")
@Slf4j
public class SqSystemAuthorizationCodeController {
    @Autowired
    private SqSystemAuthorizationCodeService sqSystemAuthorizationCodeService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sqSystemAuthorizationCodeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SqSystemAuthorizationCodeEntity sqSystemAuthorizationCode = sqSystemAuthorizationCodeService.getById(id);

        return R.ok().put("sqSystemAuthorizationCode", sqSystemAuthorizationCode);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SqSystemAuthorizationCodeEntity sqSystemAuthorizationCode){
		sqSystemAuthorizationCodeService.save(sqSystemAuthorizationCode);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody SqSystemAuthorizationCodeEntity sqSystemAuthorizationCode){
		sqSystemAuthorizationCodeService.updateById(sqSystemAuthorizationCode);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		sqSystemAuthorizationCodeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 校验接口
     */
    @ApiOperation("授权码校验")
    @PostMapping("/verify")
    public ResponseData verify(@RequestBody VerifyDto verifyDto){
        try {
            QueryWrapper<SqSystemAuthorizationCodeEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("system_code", verifyDto.getSystemCode());
            queryWrapper.eq("system_name", verifyDto.getSystemName());
            queryWrapper.eq("system_edition", verifyDto.getSystemEdition());
            log.info(verifyDto.toString());
            SqSystemAuthorizationCodeEntity codeEntity = sqSystemAuthorizationCodeService.getOne(queryWrapper);
            if(codeEntity != null) {
                if(codeEntity.getUserId() != null && codeEntity.getUsed() == 1) {
                    return ResponseData.success(3, "激活码已注册，请重新输入");
                }
                codeEntity.setUsed(1).setUsedTime(new Date());
                sqSystemAuthorizationCodeService.updateById(codeEntity);
                return ResponseData.success(1, "激活成功");
            }
            return ResponseData.error(2, "激活码错误，请重新输入");
        } catch (Exception e) {
            return ResponseData.error(-99, "授权码校验失败, 该系统版本存在两个及以上相同的授权码");
        }

    }

}
